package xnj.lazydog.btcontroller.MyViews;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class SwitchButton extends View {

    int trackColor=0xffb3b3b3;
    int trackEdgeColor=0xffe3e3e3;
    int thumbColor=0xff64C5FF;
    public int thumbOffColor=0xffffffff;
    int thumbEdgeColor=0xffeeeeee;
    float strokeWidthRatio = 0.1f;


    Paint p;

    boolean isChecked = false;
    public interface  OnCheckedChangedListener
    {
        void onCheckedChanged(boolean isChecked);
    }

    OnCheckedChangedListener listener;
    public void setOnCheckedChangedListener(OnCheckedChangedListener listener)
    {
        this.listener= listener;
    }

    public SwitchButton(Context context, AttributeSet attributeSet)
    {
        super(context,attributeSet);
        isChecked =false;
        p = new Paint();
        p.setAntiAlias(true);
    }

    float w,h;
    float check_x =0.5f;

    float check_x_set = 0.25f;
    float thumbRadiusRatio = 0.4f;
    float trackWidthRatio =0.7f;
    float trackHeightRatio = 0.5f;
    RectF rectF=new RectF(0,0,0,0);
    float delta=0;
    @Override
    protected void onDraw(Canvas canvas) {
        w = getMeasuredWidth();
        h = getMeasuredHeight();


        if(w<h*2)
        {
            trackHeightRatio = w/h*0.25f;
            thumbRadiusRatio =  w/h*0.2f;
        }


        delta=(check_x_set-check_x);

        if(Math.abs(delta)>0.01f)
            delta*=0.2f;

        check_x+=delta;


        rectF.left = w*(0.5f-trackWidthRatio*0.5f);
        rectF.top = h*(0.5f-trackHeightRatio*0.5f);
        rectF.right=w*(0.5f+trackWidthRatio*0.5f);
        rectF.bottom = h*(0.5f+trackHeightRatio*0.5f);
        p.setColor(trackColor);
        canvas.drawRoundRect(rectF,h*trackHeightRatio*0.5f,h*trackHeightRatio*0.5f,p);


        if(isChecked)
            p.setColor(thumbColor);
        else
            p.setColor(thumbOffColor);
        canvas.drawCircle(check_x*w,h*0.5f,h*thumbRadiusRatio,p);

        invalidate();
    }

    public void setCheckImm(boolean isChecked)
    {
        this.isChecked = isChecked;
        if(isChecked) {
            check_x_set = 0.75f;
            check_x = 0.75f;
        }else {
            check_x = 0.25f;
            check_x_set = 0.25f;
        }
        if(listener!=null)
            listener.onCheckedChanged(isChecked);
    }

    public void setChecked(boolean isChecked)
    {
        this.isChecked = isChecked;
        if(isChecked)
            check_x_set =0.75f;
        else
            check_x_set =0.25f;
        if(listener!=null)
            listener.onCheckedChanged(isChecked);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if(event.getAction()==MotionEvent.ACTION_DOWN)
        {
            setChecked(!isChecked);
            if(listener!=null)
                listener.onCheckedChanged(isChecked);
        }
        return true;
    }
}
